package com.scnu.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtil {
    private NumberUtil() {
    }
    //检验输入字符串是否是合法的电话号码
    public static boolean isPhoneNumber(String phone){
        Pattern pattern = Pattern.compile("^((13[0-9])|(14[5,7])|(15[^4,\\D])|(17[0-8])|(18[0-9]))\\d{8}$");
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    //生成指定长度的随机数
    public static int getRandomNum(int length){
        int num = 1;
        double random = Math.random();
        if(random < 0.1)
            random += 0.1;
        for(int i = 0; i < length; ++i)
            num *= 10;
        return (int)(num * random);
    }

    //生成订单流水号
    public static String getOrderNo(){
        StringBuffer stringBuffer = new StringBuffer(String.valueOf(System.currentTimeMillis()));
        int num = getRandomNum(4);
        stringBuffer.append(num);
        return stringBuffer.toString();
    }
}
